<?php

namespace Form;



use App\Dao\System\SystemConfigClassifyDao;
use Yj\Apidog\Annotation\ApiController;
use Yj\Apidog\Annotation\FormData;
use Yj\Apidog\Annotation\PostApi;
use Yj\ColumnBuilder\Column;
use Yj\Form\Elm;

/**
 * @ApiController(prefix="form/SystemConfig")
 */
class SystemConfig extends BaseForm
{
    protected $yjCode = 'config';

    protected $routes = [
        'lists' => '/admin/system/config/lists',
        'add' => '/admin/system/config/add',
        'edit' => '/admin/system/config/edit',
        'form' => '/form/SystemConfig/update',
    ];

    public function getColumn()
    {
        $colum = Column::create();
        $colum->setRules([
            Column::txt()->setLabel('配置ID')->setProp('config_id'),
            Column::txt()->setLabel('配置名称')->setProp('name'),
            Column::txt()->setLabel('配置key')->setProp('key'),
            Column::txt()->setLabel('描述')->setProp('info'),
            Column::switch()->setLabel('状态')->setProp('status'),
        ]);
        return $colum->toArray();
    }


    protected function getRoute($types = null)
    {
        if (is_null($types)) {
            return $this->routes;
        }
        return $this->routes[$types];
    }

    /**
     * @PostApi(path="config",description="配置")
     */
    public function config()
    {
        $routes = $this->getRoute();
        $column = $this->getColumn();
        _SUCCESS(compact('routes', 'column'));
    }

    /**
     * @PostApi(path="update",description="添加")
     * @FormData(key="config_id",default="0")
     */
    public function update()
    {
        $id = $this->getValidatorData('config_id');
        $form = Elm::createForm($this->getRoute($id ? 'edit' : 'add'));
        $formData = App(SystemConfigClassifyDao::class)->get();
        $formData = $formData->transform(function ($item) {
            return [
                'id'=>$item->id,
                'pid'=>$item->pid,
                'label' => $item['name'],
                'value' => $item['id'],
            ];
        })->toArray();
        $formData = list_to_tree($formData);
        $form->setRule([
//            ->data(function () {
//                $formData->prepend(['label' => '无', 'value' => 0]);
//                return $formData->toArray();
//            })

            Elm::treeSelected('classify_id', '上级分类')
                ->checkStrictly(true)
                ->required()
                ->defaultExpandAll(true)->data($formData),
            Elm::input('type', '类型')->required(),
            Elm::input('name', '配置名称')->required(),
            Elm::input('key', '配置key')->required(),
            Elm::input('info', '描述')->required(),
            Elm::number('sort', '排序')->required(),
            Elm::switches('status', '状态')->activeValue(1)->inactiveValue(0)->required(),
        ]);

        $id && $form->append(
            Elm::hidden('config_id', $id)
        );
        $lists = $form->setTitle(($id ? '编辑' : '添加') . '分类')->formData(function () use ($id) {
            $formData = [];
            if ($id) {
                $formData = app(\App\Model\System\SystemConfig::class)
                    ->where('config_id', $id)->first();
            }
            return $formData;
        });
        _SUCCESS(formToData($lists));
    }
}